{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "51092bed",
   "metadata": {},
   "source": [
    "## 上周回顾\n",
    "> * 1. list的一般方法/函数的使用及案例，如：\n",
    ">> 1. append() 增加，增加list元素到list的末尾，常用于新建列表\n",
    ">> 2. pop(index)/remove(values) 对比两者优势\n",
    ">> 3. count() 查看某个元素出现的次数\n",
    ">> 4. index() 查看某个元素第一次出现的位置\n",
    ">> 5. entend() 两个list合并在一起，也可用[]+[]来实现\n",
    ">> 6. sort() list元素的排序\n",
    "\n",
    "> * 2. list的高级用法，如：\n",
    ">> 1. copy() list（可变的数据类型）复制不等价于赋值 可用[:]来实现\n",
    ">> 2. enumerate() 枚举，可同时遍历并获取index和values\n",
    "\n",
    "## 本周内容：\n",
    "### list练习，vowels.py vowels2.py\n",
    "### list实践："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ee9ff7f",
   "metadata": {},
   "source": [
    "### list练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b7b6771",
   "metadata": {},
   "source": [
    "> * vowels.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fd622dca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = \"Milliways\"\n",
    "# 检查word中出现的元音，并打印出来\n",
    "# for letter in word:  # 第一个元素letter 请不要和已有变量重名\n",
    "                       # 第一个元素一般不定义名字，常用item（元素）来命名\n",
    "                       #                           简写i来命名\n",
    "                       # 第二个元素word（重要），意味着将遍历的内容\n",
    "                       # 遍历的内容一般是 序列数据 str list\n",
    "for letter in word:\n",
    "#    print(letter)\n",
    "    if letter in vowels:\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcadde7f",
   "metadata": {},
   "source": [
    "> * vowels2.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "66c6fa65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['i', 'a']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用append()新建列表\n",
    "found = []\n",
    "for letter in word:\n",
    "#   print(letter)\n",
    "    if letter in vowels:  # letter在元音字母表中\n",
    "        if letter not in found: # letter不在found表中\n",
    "            found.append(letter)\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82d3d52f",
   "metadata": {},
   "source": [
    "### list实践："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0f00218",
   "metadata": {},
   "source": [
    "购物车程序项目：\n",
    "\n",
    "要求：\n",
    "\n",
    "1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6d76aeb6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的支付宝余额:80000\n",
      "------请选择编号 欢迎选购------\n",
      "商品编号：0,商品名称：Jackson Wang's concert tickets,商品价格：8520 元\n",
      "商品编号：1,商品名称：iphone 13 pro Max,商品价格：10000 元\n",
      "商品编号：2,商品名称：Jacket of Team Wang design,商品价格：1000 元\n",
      "请输入商品编号（如果退出请输入“q”）：1\n",
      "【iphone 13 pro Max】已经加入您的购物车，您的【账户余额】还有 70000 元\n",
      "------请选择编号 欢迎选购------\n",
      "商品编号：0,商品名称：Jackson Wang's concert tickets,商品价格：8520 元\n",
      "商品编号：1,商品名称：iphone 13 pro Max,商品价格：10000 元\n",
      "商品编号：2,商品名称：Jacket of Team Wang design,商品价格：1000 元\n",
      "请输入商品编号（如果退出请输入“q”）：0\n",
      "【Jackson Wang's concert tickets】已经加入您的购物车，您的【账户余额】还有 61480 元\n",
      "------请选择编号 欢迎选购------\n",
      "商品编号：0,商品名称：Jackson Wang's concert tickets,商品价格：8520 元\n",
      "商品编号：1,商品名称：iphone 13 pro Max,商品价格：10000 元\n",
      "商品编号：2,商品名称：Jacket of Team Wang design,商品价格：1000 元\n",
      "请输入商品编号（如果退出请输入“q”）：2\n",
      "【Jacket of Team Wang design】已经加入您的购物车，您的【账户余额】还有 60480 元\n",
      "------请选择编号 欢迎选购------\n",
      "商品编号：0,商品名称：Jackson Wang's concert tickets,商品价格：8520 元\n",
      "商品编号：1,商品名称：iphone 13 pro Max,商品价格：10000 元\n",
      "商品编号：2,商品名称：Jacket of Team Wang design,商品价格：1000 元\n",
      "请输入商品编号（如果退出请输入“q”）：q\n",
      "您的购物车： [['iphone 13 pro Max', 10000], [\"Jackson Wang's concert tickets\", 8520], ['Jacket of Team Wang design', 1000]]\n",
      "您的余额：60480 元\n"
     ]
    }
   ],
   "source": [
    "# 商品信息的数据存放（商品编号，商品名称，商品价格）\n",
    "goods_list = [\n",
    "    [\"Jackson Wang's concert tickets\",8520],  # 我们认为每一个商品内容是一个多个值的值，可选用list\n",
    "    [\"iphone 13 pro Max\",10000],\n",
    "    [\"Jacket of Team Wang design\",1000]\n",
    "]\n",
    "\n",
    "# 用户购物信息的数据存放\n",
    "user_shopping_cart = []  # 用户列表初始为空，后续增加可考虑用append()方法\n",
    "\n",
    "# 1.用户输入余额信息，打印商品列表给用户\n",
    "Alipay = int(input(\"请输入您的支付宝余额:\"))  # input的使用，结果是一个字符串\n",
    "\n",
    "# 打印商品信息,打印商品的次数由商品列表的长度决定（len(goods_list)）\n",
    "# info = '''------goods list------\n",
    "# 商品编号：%s,商品名称：%s,商品价格：%s元\n",
    "# 商品编号：%s,商品名称：%s,商品价格：%s元\n",
    "# 商品编号：%s,商品名称：%s,商品价格：%s元\n",
    "# '''%(0,goods_list[0][0],goods_list[0][1],\n",
    "#      1,goods_list[1][0],goods_list[1][1],\n",
    "#      2,goods_list[2][0],goods_list[2][1],\n",
    "#      3,goods_list[3][0],goods_list[3][1],\n",
    "# )\n",
    "# print(info)\n",
    "\n",
    "# list长度 len()\n",
    "\n",
    "# 执行循环\n",
    "while True: # 循环遍历商品列表，将商品信息按照一定格式输出\n",
    "    print('------请选择编号 欢迎选购------')\n",
    "    for i in range(len(goods_list)):  # range会根据商品列表的长短变化而变化\n",
    "        # 给格式化的样式（先给）\n",
    "        # 样式（不懂代码的人可以完成的工作）：商品编号：xx，商品名称：xx，商品价格：xx\n",
    "        print(\"商品编号：%s,商品名称：%s,商品价格：%s 元\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "    # 2.用户输入商品编号，实现商品查询购买\n",
    "    choice = input(\"请输入商品编号（如果退出请输入“q”）：\") # input输入的内容默认是字符串str 例如输入2，则显示“2”\n",
    "    if choice.isdigit():  # isdigit判断字符串里面的内容是否是数值\n",
    "        choice = int(choice) # 如果是数值，把它变成真正的int\n",
    "        # 判断商品编号是否在范围内\n",
    "        if int(choice) > len(goods_list):\n",
    "            print(\"您输入的商品不存在，请检查再次输入\")\n",
    "        else:\n",
    "            # 对比商品编号，找到对应商品：商品价格 goods_list[choice][1],\n",
    "            # 3.检查用户余额 VS 商品价格关系\n",
    "            if Alipay > goods_list[choice][1]:  # 买得起 1. 扣钱 2.把商品加到用户购物列表\n",
    "                Alipay = Alipay-goods_list[choice][1]  \n",
    "                user_shopping_cart.append(goods_list[choice])\n",
    "                print(\"【%s】已经加入您的购物车，您的【账户余额】还有 %s 元\"%(goods_list[choice][0],Alipay))\n",
    "            else:\n",
    "                print(\"您的余额已不足，请更换支付方式\")\n",
    "    elif choice == \"q\":\n",
    "        # 如果用户输入q，则说明用户要退出购买了，我们将打印用户已购买的商品列表以及用户余额\n",
    "        print(\"您的购物车：\",user_shopping_cart)\n",
    "        print(\"您的余额：%s 元\"%(Alipay))\n",
    "        break\n",
    "    else:\n",
    "        print(\"请输入正确的【商品编号】！或者按照输入提示进行输入\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
